home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWDebug / Sources / FWDbgStr.cpp next >
Encoding:
Text File  |  1995-11-08  |  10.7 KB  |  367 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWDbgStr.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #include <stddef.h>
  13.  
  14. // Need to include first so that FW_DEBUG is properly defined
  15. #ifndef FWDBGSTR_H
  16. #include "FWDbgStr.h"
  17. #endif
  18.  
  19. #ifdef FW_DEBUG
  20.  
  21. #ifndef FWPRIMEM_H
  22. #include "FWPriMem.h"
  23. #endif
  24.  
  25. #ifndef FWPRISTR_H
  26. #include "FWPriStr.h"
  27. #endif
  28.  
  29. #ifndef FWPRIDEB_H
  30. #include "FWPriDeb.h"
  31. #endif
  32.  
  33. #if defined(FW_BUILD_MAC) && !defined(__FILES__)
  34. #include <Files.h>
  35. #endif
  36.  
  37. #if defined(FW_BUILD_MAC) && !defined(__ERRORS__)
  38. #include <Errors.h>
  39. #endif
  40.  
  41. #if defined(FW_BUILD_MAC) && !defined(__TEXTUTILS__)
  42. #include <TextUtils.h>
  43. #endif
  44.  
  45. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  46. #include <Script.h>
  47. #endif
  48.  
  49. #if FW_LIB_EXPORT_PRAGMAS
  50. #pragma lib_export on
  51. #endif
  52.  
  53. #ifdef FW_BUILD_MAC
  54. #pragma segment FWDebug
  55. #endif
  56.  
  57. //========================================================================================
  58. //    CLASS FW_CDebugStream
  59. //========================================================================================
  60.  
  61. //----------------------------------------------------------------------------------------
  62. // FW_CDebugStream::EndLine
  63. //----------------------------------------------------------------------------------------
  64.  
  65. FW_FUNC_ATTR FW_CDebugStream& EndLine(FW_CDebugStream &stream)
  66. {
  67. #ifdef FW_BUILD_MAC
  68.     return stream.Write((void *) "\n", sizeof(char));
  69. #endif
  70. #ifdef FW_BUILD_WIN
  71.     return stream.Write((void *) "\n\r", sizeof(char) * 2);
  72. #endif
  73. }
  74.  
  75. //----------------------------------------------------------------------------------------
  76. // FW_CDebugStream::FW_CDebugStream
  77. //----------------------------------------------------------------------------------------
  78.  
  79. FW_CDebugStream::FW_CDebugStream()
  80. {
  81. }
  82.  
  83. //----------------------------------------------------------------------------------------
  84. // FW_CDebugStream::~FW_CDebugStream
  85. //----------------------------------------------------------------------------------------
  86.  
  87. FW_CDebugStream::~FW_CDebugStream()
  88. {
  89. }
  90.  
  91. //----------------------------------------------------------------------------------------
  92. // FW_CDebugStream::WriteChunk
  93. //----------------------------------------------------------------------------------------
  94.  
  95. FW_CDebugStream &FW_CDebugStream::WriteChunk(const void *data, size_t size)
  96. {
  97.     Write(data, size);
  98.     return Write((void *) " ", sizeof(char));
  99. }
  100.  
  101. //----------------------------------------------------------------------------------------
  102. // FW_CDebugStream::WriteBase10Number(long n)
  103. //----------------------------------------------------------------------------------------
  104.  
  105. FW_CDebugStream & FW_CDebugStream::WriteBase10Number(long n)
  106. {
  107.     char buf[20];
  108.     char *s = &buf[20];
  109.  
  110.     *--s = 0;
  111.     do
  112.     {
  113.         *--s = (char) (n % 10) + '0';
  114.         n /= 10;
  115.     } while (n != 0);
  116.  
  117.     return WriteChunk((void *) s, FW_PrimitiveStringLength(s));
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. // FW_CDebugStream::WriteBase16Number(long n)
  122. //----------------------------------------------------------------------------------------
  123.  
  124. FW_CDebugStream & FW_CDebugStream::WriteBase16Number(unsigned long n)
  125. {
  126.     const char *digits = "0123456789ABCDEF";
  127.     char buf[11] = "0x00000000";
  128.     char *s = &buf[11];
  129.     
  130.     for (; n != 0; n /= 16)
  131.         *--s = digits[(short) (n % 16)];
  132.  
  133.     return WriteChunk((void *) buf, 10);
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // FW_CDebugStream::operator<<(const signed char *string)
  138. //----------------------------------------------------------------------------------------
  139.  
  140. FW_CDebugStream & FW_CDebugStream::operator<<(const signed char *string)
  141. {
  142.     return WriteChunk((void *) string, FW_PrimitiveStringLength((const char *) string));
  143. }
  144.  
  145. //----------------------------------------------------------------------------------------
  146. // FW_CDebugStream::operator<<(signed char c)
  147. //----------------------------------------------------------------------------------------
  148.  
  149. FW_CDebugStream & FW_CDebugStream::operator<<(signed char c)
  150. {
  151.     return WriteChunk((void *) &c, sizeof(char));
  152. }
  153.  
  154. //----------------------------------------------------------------------------------------
  155. // FW_CDebugStream::operator<<(long n)
  156. //----------------------------------------------------------------------------------------
  157.  
  158. FW_CDebugStream & FW_CDebugStream::operator<<(long n)
  159. {
  160.     if (n < 0)
  161.     {
  162.         Write((void *) "-", sizeof(char));
  163.         n = -n;
  164.     }
  165.     return WriteBase10Number(n);
  166. }
  167.  
  168. //----------------------------------------------------------------------------------------
  169. // FW_CDebugStream::operator<<(unsigned long n)
  170. //----------------------------------------------------------------------------------------
  171.  
  172. FW_CDebugStream & FW_CDebugStream::operator<<(unsigned long n)
  173. {
  174.     return WriteBase10Number(n);
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. // FW_CDebugStream::operator<<(void *p)
  179. //----------------------------------------------------------------------------------------
  180.  
  181. FW_CDebugStream & FW_CDebugStream::operator<<(void *p)
  182. {
  183.     return WriteBase16Number((long) p);
  184. }
  185.  
  186.  
  187. //========================================================================================
  188. //    CLASS FW_CBufferDebugStream
  189. //========================================================================================
  190.  
  191. //----------------------------------------------------------------------------------------
  192. // FW_CBufferDebugStream::FW_CBufferDebugStream
  193. //----------------------------------------------------------------------------------------
  194.  
  195. FW_CBufferDebugStream::FW_CBufferDebugStream(void * buffer, size_t bufferSize) :
  196.     fBuffer(buffer),
  197.     fBufferSize(bufferSize),
  198.     fPosition(0)
  199. {
  200. }
  201.     
  202. //----------------------------------------------------------------------------------------
  203. // FW_CBufferDebugStream::~FW_CBufferDebugStream
  204. //----------------------------------------------------------------------------------------
  205.  
  206. FW_CBufferDebugStream::~FW_CBufferDebugStream()
  207. {
  208. }
  209.     
  210. //----------------------------------------------------------------------------------------
  211. // FW_CBufferDebugStream::Write
  212. //----------------------------------------------------------------------------------------
  213.  
  214. FW_CDebugStream &FW_CBufferDebugStream::Write(const void *data, size_t size)
  215. {
  216.     if (fPosition >= fBufferSize)
  217.         return *this;
  218.     
  219.     size_t writeAmmount = FW_Minimum(size, (size_t)(fBufferSize - fPosition));
  220.     
  221.     FW_PrimitiveCopyMemory(data, (void*)fBuffer, writeAmmount);
  222.     
  223.     return *this;
  224. }
  225.  
  226. //========================================================================================
  227. //    CLASS FW_CNullDebugStream
  228. //========================================================================================
  229.  
  230. //----------------------------------------------------------------------------------------
  231. // FW_CNullDebugStream::FW_CNullDebugStream
  232. //----------------------------------------------------------------------------------------
  233.  
  234. FW_CNullDebugStream::FW_CNullDebugStream()
  235. {
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. // FW_CNullDebugStream::~FW_CNullDebugStream
  240. //----------------------------------------------------------------------------------------
  241.  
  242. FW_CNullDebugStream::~FW_CNullDebugStream()
  243. {
  244. }
  245.  
  246. //----------------------------------------------------------------------------------------
  247. // FW_CNullDebugStream::Write
  248. //----------------------------------------------------------------------------------------
  249.  
  250. FW_CDebugStream &FW_CNullDebugStream::Write(const void *data, size_t size)
  251. {
  252. FW_UNUSED(data);
  253. FW_UNUSED(size);
  254.     return *this;
  255. }
  256.  
  257.  
  258. //========================================================================================
  259. // CLASS FW_CFileDebugStream
  260. //========================================================================================
  261.  
  262. //----------------------------------------------------------------------------------------
  263. // FW_CFileDebugStream::FW_CFileDebugStream
  264. //----------------------------------------------------------------------------------------
  265.  
  266. FW_CFileDebugStream::FW_CFileDebugStream(char *fileName)
  267. {
  268. #ifdef FW_BUILD_MAC
  269.     FSSpec spec;
  270.     
  271.     OSErr err = FSMakeFSSpec(0, 0, c2pstr(fileName), &spec);
  272.     p2cstr((unsigned char *) fileName); // c2pstr modifies the string so lets put it back the way it was
  273.     if (err == fnfErr)
  274.     {
  275.         err = FSpCreate(&spec, 'MPS ', 'TEXT', langEnglish);
  276.         if (err)
  277.         {
  278.             FW_PRIV_DEBUGGER_STRING(
  279.                 "FW_CFileDebugStream::FW_CFileDebugStream: can't create file.");
  280.         }
  281.     }
  282.     else if (err)
  283.     {
  284.         FW_PRIV_DEBUGGER_STRING(
  285.             "FW_CFileDebugStream::FW_CFileDebugStream: invalid path.");
  286.     }
  287.     
  288.     err = FSpOpenDF(&spec, fsWrPerm, &fMacFileNumber);
  289.     if (err)
  290.         FW_PRIV_DEBUGGER_STRING(
  291.             "FW_CFileDebugStream::FW_CFileDebugStream: can't open file.");
  292.     
  293.     err = SetEOF(fMacFileNumber, 0);
  294.     if (err)
  295.         FW_PRIV_DEBUGGER_STRING(
  296.             "FW_CFileDebugStream::FW_CFileDebugStream: can't reset EOF.");
  297. #endif
  298. #ifdef FW_BUILD_WIN
  299.     fWinFileHandle = _lcreat(fileName, 0);
  300.         // Create, or open and truncate a file for read and write
  301.     
  302.     if (fWinFileHandle == HFILE_ERROR)
  303.         FW_PRIV_DEBUGGER_STRING(
  304.             "FW_CFileDebugStream::FW_CFileDebugStream: can't create/open file.");
  305. #endif
  306. }
  307.  
  308. //----------------------------------------------------------------------------------------
  309. // FW_CFileDebugStream::~FW_CFileDebugStream
  310. //----------------------------------------------------------------------------------------
  311.  
  312. FW_CFileDebugStream::~FW_CFileDebugStream()
  313. {
  314. #ifdef FW_BUILD_MAC
  315.     OSErr err = FSClose(fMacFileNumber);
  316.     
  317.     if (err)
  318.         FW_PRIV_DEBUGGER_STRING(
  319.             "FW_CFileDebugStream::~FW_CFileDebugStream: can't close.");
  320. #endif
  321. #ifdef FW_BUILD_WIN
  322.     HFILE result = _lclose(fWinFileHandle);
  323.     
  324.     if (result == HFILE_ERROR)
  325.         FW_PRIV_DEBUGGER_STRING(
  326.             "FW_CFileDebugStream::~FW_CFileDebugStream: can't close.");
  327. #endif
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. // FW_CFileDebugStream::Write
  332. //----------------------------------------------------------------------------------------
  333.  
  334. FW_CDebugStream & FW_CFileDebugStream::Write(const void *data,
  335.                                             size_t size)
  336. {
  337. #ifdef FW_BUILD_MAC
  338.     long count = size;
  339.     
  340.     OSErr err = FSWrite(fMacFileNumber, &count, (Ptr) data);
  341.     if (err)
  342.     {
  343.         FSClose(fMacFileNumber);
  344.         // Go ahead and try closing the file
  345.         
  346.         FW_PRIV_DEBUGGER_STRING(
  347.             "FW_CFileDebugStream::FW_CFileDebugStreamWrite: can't write.");
  348.     }
  349. #endif
  350. #ifdef FW_BUILD_WIN
  351.     long written = _hwrite(fWinFileHandle, (const char*) data, size);
  352.  
  353.     if (written == -1L)
  354.     {
  355.         _lclose(fWinFileHandle);
  356.             // Go ahead and try closing the file
  357.         
  358.         FW_PRIV_DEBUGGER_STRING(
  359.             "FW_CFileDebugStream::FW_CFileDebugStreamWrite: can't write.");
  360.     }
  361. #endif
  362.     
  363.     return *this;
  364. }
  365.  
  366. #endif // FW_DEBUG
  367.